package top.kpromise.user.utils;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.ResponseBody;

import javax.net.ssl.*;

public class OkHttpUtils {

    private static OkHttpClient okHttpClient;

    private static OkHttpClient okHttpClient() {
        if (okHttpClient == null) {
            okHttpClient = new OkHttpClient();
        }
        return okHttpClient;
    }

    public static <T> T send(Request request, Class<T> clazz) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.readValue(responseString(request), clazz);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static JsonNode send(Request request) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.readTree(responseString(request));
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private static String responseString(Request request) {
        try {
            OkHttpClient okHttpClient = okHttpClient();
            ResponseBody responseBody = okHttpClient.newCall(request).execute().body();
            if (responseBody != null) {
                return responseBody.string();
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static OkHttpClient getUnsafeOkHttpClient() {
        try {
            // Create a trust manager that does not validate certificate chains
            final TrustManager[] trustAllCerts = new TrustManager[] {
                    new X509TrustManager() {
                        @Override
                        public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType)  {
                        }

                        @Override
                        public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType)  {
                        }

                        @Override
                        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                            return new java.security.cert.X509Certificate[]{};
                        }
                    }
            };

            // Install the all-trusting trust manager
            final SSLContext sslContext = SSLContext.getInstance("SSL");
            sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
            // Create an ssl socket factory with our all-trusting manager
            final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

            OkHttpClient.Builder builder = new OkHttpClient.Builder();
            builder.sslSocketFactory(sslSocketFactory);
            builder.hostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            });
            return  builder.build();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
